﻿/*
 * Mike McBride & Dave Rank's
 * Stop Watch / Timer Mobile Application
 * 
 * Thread example learned from Maarten Struys, off MSDN
 * http://msdn.microsoft.com/en-us/netframework/cc307206.aspx
 * Stopwatch functionality added by team
 */

using System;
using System.Threading;

namespace StopWatch.Controller
{
    using View;

    public class StopWatch
    {
        private readonly IStopWatchUI _stopWatchUI; // UI interface
        private Thread _workerThread; // Thread that does the counting
        private DateTime _lapTimestamp;

        public StopWatch(IStopWatchUI stopWatchUI)
        {
            _stopWatchUI = stopWatchUI;
            TimerStopped = true;
            CountUp = true;
        }

        public bool TimerStopped { get; private set;}

        public bool CountUp { get; set; }

        public bool LoggingEnabled { get; set;}

        public DateTime StartTime { get; private set; }

        public DateTime CurrentTime { get; private set; }

        public DateTime EndTime { get; private set; }

        public TimeSpan ElapsedTime
        {
            get
            {
                return (CurrentTime - StartTime);
            }
        }

        public TimeSpan LapTime { get; private set; }

        public string Username { get; set; }

        public void Start()
        {
            StartTime = DateTime.Now;
            TimerStopped = false;
            _stopWatchUI.UpdateStartDisplay();
            _workerThread = new Thread(WorkerThread);
            _workerThread.Start();
        }

        public void Stop()
        {
            EndTime = DateTime.Now;
            TimerStopped = true;
            _stopWatchUI.UpdateStopDisplay();
            _workerThread.Abort();

            if(LoggingEnabled)
                LogCurrent();
        }

        public void Lap()
        {
            LapTime = (LapTime == TimeSpan.Zero) ? ElapsedTime : ElapsedTime - (_lapTimestamp - StartTime);
            _lapTimestamp = DateTime.Now;
        }

        public void Reset()
        {
            LapTime = TimeSpan.Zero;
        }

        public void LogCurrent()
        {
            PostSubmitter pSub = new PostSubmitter("http://mdse4910.onlinestudentresearch.com/timeLogger.php");
            pSub.PostItems.Add("username", Username);
            pSub.PostItems.Add("date", DateTime.Today.ToLongDateString());
            pSub.PostItems.Add("startTime", StartTime.ToLongTimeString());
            pSub.PostItems.Add("endTime", EndTime.ToLongTimeString());
            pSub.PostItems.Add("elapsedTime", ElapsedTime.ToString());
            pSub.Type = PostSubmitter.PostTypeEnum.Post;
            pSub.Post();
        }

        public void ResetLog()
        {
            PostSubmitter pSub = new PostSubmitter("http://mdse4910.onlinestudentresearch.com/logKill.php");
            pSub.PostItems.Add("auth", "b4L3e73|)");
            pSub.Type = PostSubmitter.PostTypeEnum.Post;
            pSub.Post();
        }

        //Timer thread
        private void WorkerThread()
        {
            while (!TimerStopped)
            {
                CurrentTime = DateTime.Now; //Get current time

                // We no longer need to manually set the current time; it is dynamically caluclated when requested
                // (see CurrentTime property)

                // Inform the UI that it needs to update its displays
                // Any invoking must be done from the UI!
                _stopWatchUI.UpdateElapsedDisplay();
                _stopWatchUI.UpdateLapDisplay();

                Thread.Sleep(75); //Cadence updating the thread
            }
        } //end WorkerThread
    }
}
